﻿using System;
using System.Collections.Generic;
using System.Configuration;
using RN.ERP.Web.ModelService;

namespace RN.ERP.Web
{
    public partial class MainFrame : System.Web.UI.Page
    {
        #region 系统参数

        public string AdSwitch = ConfigurationManager.AppSettings["AdvertSwitch"];

        public string PageTitle
        {
            get
            {
                return ConfigurationManager.AppSettings["sysTitle"];
            }
        }

        public string SystemID
        {
            get
            {
                return ConfigurationManager.AppSettings["AppId"];
            }
        }

        /// <summary>
        /// 是否加载站内信模块
        /// </summary>
        public string IsSiteLetterUsable
        {
            get
            {
                return ConfigurationManager.AppSettings["IsSiteLetterUsable"];
            }
        }

        /// <summary>
        /// 是否加载即时通讯模块
        /// </summary>
        public string IsChatModuleUsable
        {
            get
            {
                return ConfigurationManager.AppSettings["IsChatModuleUsable"];
            }
        }

        protected SessionUserInfo UserInfo
        {
            get
            {
                if (Session[SessionConstant.SESSION_USERINFO] == null)
                {
                    RebuildSessionUserInfo();
                }
                return Session[SessionConstant.SESSION_USERINFO] == null ? null : (SessionUserInfo)Session[SessionConstant.SESSION_USERINFO];
            }
        }

        /// <summary>
        /// 上次请求的URL
        /// </summary>
        public string LoginPageUrl
        {
            get
            {
                return Request.UrlReferrer == null ? "" : Request.UrlReferrer.ToString();
            }
        }
        
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                myStyleSheet.Attributes.Add("href", ConfigurationManager.AppSettings["AppStyle"] + "css/style.css");
                GetUserInfoTip();
                AddSiteLetterModule();
                AddChatModule();
                if (!string.IsNullOrEmpty(SystemID) && SystemID == "95")
                {
                    GetExpertConfig();
                }
            }
        }

        /// <summary>
        /// 添加站内信模块
        /// </summary>
        private void AddSiteLetterModule()
        {
            if (IsSiteLetterUsable == "true")
            {
                this.ClientScript.RegisterClientScriptInclude("siteletter", "Scripts/Message/js/MainFrame_SiteLetter.js");
            }
        }

        private void AddChatModule()
        {
            if (IsChatModuleUsable == "true")
            {
                this.ClientScript.RegisterClientScriptInclude("chat", "Scripts/Message/js/MainFrame_Chat.js");
            }
        }

        /// <summary>
        /// 重构用户Session数据。
        /// </summary>
        private void RebuildSessionUserInfo()
        {
            UtilitiesTool.RebuildSessionUserInfo();
        }
        /// <summary>
        /// 取得用户登录提示信息
        /// </summary>
        private void GetUserInfoTip()
        {
            try
            {
                if (UserInfo.RoleType.ToLower() == "user")
                {
                    //RadToolTip1.TargetControlID = userInfoTop.ClientID;//加提示

                    ////绑定当前市场
                    if (string.IsNullOrEmpty(UserInfo.CurrentArea))
                    {
                        ltrCurrentMarket.Text = "未添加";
                    }
                    else
                    {
                        ltrCurrentMarket.Text = UtilitiesTool.GetAreaTitle(UserInfo.CurrentArea);
                    }
                    liMarkinfo.Visible = true;
                    liUserInfo.Visible = true;
                }
                else
                {
                    liMarkinfo.Visible = false;
                    liUserInfo.Visible = false;
                }
                ltrLoginUser.Text = UserInfo.UserName;//用户名
                ltrLoginCorpName.Text = UserInfo.Name;//企业名称
                ltrLoginName.Text = UserInfo.Name;
                Dictionary<string, string> CorpTypeDic = UtilitiesTool.CorpTypeDic;
                ltrLoginCorpType.Text = CorpTypeDic[UserInfo.RoleId.ToString()];//用户类型  
            }
            catch { }
        }

        /// <summary>
        /// 专家系统配置
        /// </summary>
        private void GetExpertConfig()
        {
            ltrLoginName.Visible = false;
            liUserInfo.Visible = false;
            ltrCurrentMarket.Visible = false;
            liforward.Visible = false;
            liback.Visible = false;
            liuser.Visible = true;
            lihelp.Visible = false;
            liqq.Visible = false;
            liUserName.Visible = false;
        }

        //#region 主导航
        ///// <summary>
        ///// 加一级菜单
        ///// </summary>
        //protected void InitMenu()
        //{
        //    if (UserInfoHandler == null)
        //    {
        //        return;
        //    }

        //    List<AuthorityService.T_Sys_Function> sysFunctions = null;
        //    using (AuthorityService.AuthorityServiceClient client = new AuthorityService.AuthorityServiceClient())
        //    {
        //        sysFunctions = JsonConvert.DeserializeObject<List<AuthorityService.T_Sys_Function>>(client.GetUserMenus(UserInfoHandler.CorpUserGuid, ""));
        //    }
        //    var sysFunction = sysFunctions.Where(f => f.ParenGuid == Guid.Empty).ToList();
        //    foreach (AuthorityService.T_Sys_Function sysFunc in sysFunction)
        //    {
        //        //Rad菜单项
        //        RadPanelItem item = new RadPanelItem();
        //        // item.CssClass = "select";
        //        item.Text = sysFunc.Title;
        //        item.Value = sysFunc.FunctionGuid.ToString();
        //        //item.NavigateUrl = sysFunc.Url;

        //        //子菜单
        //        if (sysFunctions.Where(f => f.ParenGuid == sysFunc.FunctionGuid).Count() > 0)
        //        {
        //            InitSubMenu(item, sysFunc.FunctionGuid, sysFunc.Title, sysFunctions);
        //        }
        //        item.ExpandParentItems();
        //        this.rpbMenu.Items.Add(item);
        //    }
        //}

        ///// <summary>
        ///// 加子菜单模板容器
        ///// </summary>
        ///// <param name="item"></param>
        ///// <param name="FunctionGuid"></param>
        ///// <param name="topMenuName"></param>
        ///// <param name="sysFunctions"></param>
        //private void InitSubMenu(RadPanelItem item, Guid FunctionGuid, string topMenuName, List<AuthorityService.T_Sys_Function> sysFunctions)
        //{
        //    var sysFunction = sysFunctions.Where(f => f.ParenGuid == FunctionGuid).ToList();
        //    foreach (var child in sysFunction)
        //    {
        //        RadPanelItem childItem = new RadPanelItem();
        //        childItem.Text = child.Title;
        //        string url = child.Url.Replace("~", "..");
        //        childItem.Attributes["onclick"] = string.Format("navigateUrl(this,'{0}','{1}');", child.Title, url);
        //        //childItem.NavigateUrl = child.Url;
        //        item.Items.Add(childItem);
        //    }
        //}

        ///// <summary>
        ///// 加子菜单数据
        ///// </summary>
        ///// <param name="sm"></param>
        ///// <param name="node"></param>
        ///// <param name="FunctionGuid"></param>
        ///// <param name="sysFunctions"></param>
        //private static void PopulateSiteMap(RadSiteMap sm, RadSiteMapNode node, Guid FunctionGuid, List<AuthorityService.T_Sys_Function> sysFunctions)
        //{
        //    var sysFunction = sysFunctions.Where(f => f.ParenGuid == FunctionGuid).ToList();
        //    if (node == null)
        //    {
        //        foreach (var child in sysFunction)
        //        {
        //            node = new RadSiteMapNode(child.Title, child.Url);
        //            sm.Nodes.Add(node);
        //            if (sysFunctions.Where(f => f.ParenGuid == child.FunctionGuid).Count() > 0)
        //            {
        //                PopulateSiteMap(sm, node, child.FunctionGuid, sysFunctions);
        //            }
        //        }
        //    }
        //    else
        //    {
        //        foreach (var child in sysFunction)
        //        {
        //            node.Nodes.Add(new RadSiteMapNode(child.Title, child.Url));
        //            if (sysFunctions.Where(f => f.ParenGuid == child.FunctionGuid).Count() > 0)
        //            {
        //                PopulateSiteMap(sm, node, child.FunctionGuid, sysFunctions);
        //            }
        //        }
        //    }
        //}

        //#endregion
    }
}
